
use std::time::Duration;

use serde::de::DeserializeOwned;

use tracing_appender::{non_blocking, rolling};
use reqwest::{self,Client};
use reqwest::header::{HeaderMap};
use crate::{error::{Result}};

pub fn http_make_headers(headers:Vec<(&'static str,&'static str)>) -> HeaderMap{
    let mut hm = HeaderMap::new();
    if ! headers.is_empty(){
        for x in headers {      
            hm.insert(x.0, x.1.parse().unwrap());
        }
    }
    hm
}

pub async fn http_get(url:&str,headers:Vec<(&'static str,&'static str)>,timeout:u64) -> Result<String>{
    let client = Client::new().get(url).headers(http_make_headers(headers)).timeout(Duration::from_secs(timeout));
    let resp = client.send().await?.text().await?;
    Ok(resp)
}

pub async fn http_get_json<T: DeserializeOwned>(url:&str,headers:Vec<(&'static str,&'static str)>,timeout:u64) -> Result<T>
{
    let client = Client::new().get(url).headers(http_make_headers(headers)).timeout(Duration::from_secs(timeout));
    let resp = client.send().await?.json::<T>().await?;
    Ok(resp)
}

